package com.itheima.controller;

import com.itheima.common.PageResult;
import com.itheima.common.R;
import com.itheima.common.ResultData;
import com.itheima.domain.PmsProductCategory;
import com.itheima.manager.PmsProductCategoryManager;
import com.itheima.vo.PmsProductCategoryVo;
import com.itheima.vo.PmsProductCategoryWithChildrenItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")
public class productCategoryController {
    @Autowired
    PmsProductCategoryManager pmsProductCategoryManager;

    //分页查询商品分类
    @GetMapping("/productCategory/list/{parentId}")
    public ResultData list(@PathVariable Integer parentId,
                            @RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult pageResult = pmsProductCategoryManager.pageByParentId(parentId, pageNum, pageSize);
        return ResultData.ok(pageResult, "");
    }

    //根据id获取商品分类
    @GetMapping("/productCategory/{id}")
    public ResultData selectById(@PathVariable Integer id) {
        PmsProductCategory pmsProductCategory = pmsProductCategoryManager.selectById(id);
        return ResultData.ok(pmsProductCategory, "");
    }

    //添加商品分类
    @PostMapping("/productCategory/create")
    public ResultData create(@RequestBody PmsProductCategory pmsProductCategory) {
        pmsProductCategoryManager.create(pmsProductCategory);
        return ResultData.ok(null, null);
    }

    //删除商品分类
    @PostMapping("/productCategory/delete/{id}")
    public ResultData deleteById(@PathVariable Integer id) {
        pmsProductCategoryManager.deleteById(id);
        return ResultData.ok(null, null);
    }

    //修改商品分类
    @PostMapping("/productCategory/update/{id}")
    public ResultData updateById(@PathVariable Integer id, @RequestBody PmsProductCategory productCategoryController) {
        productCategoryController.setId((long) id);
        pmsProductCategoryManager.updateById(productCategoryController);
        return ResultData.ok(null, null);
    }

    //修改导航栏显示状态
    @PostMapping("/productCategory/update/navStatus")
    public ResultData updateNavStatus(@RequestParam("ids") ArrayList<Integer> ids, Integer navStatus) {
        pmsProductCategoryManager.updateNavStatus(ids,navStatus);
        return ResultData.ok(null, null);
    }

    //修改显示状态
    @PostMapping("/productCategory/update/showStatus")
    public ResultData updateShowStatus( @RequestParam ArrayList<Integer> ids, Integer showStatus) {
        pmsProductCategoryManager.updateShowStatus(ids,showStatus);
        return ResultData.ok(null, null);
    }


//    //
//    @GetMapping("/productCategory/list/withChildren")
//    public ResultData withChildren() {
//        List<PmsProductCategoryWithChildrenItem> list1 = pmsProductCategoryManager.getWithChildren();
//        return ResultData.ok(list1, "null");
//    }
    //查询所有一级分类及子分类
    @GetMapping("/productCategory/list/withChildren")
    public ResultData withChildren(){
        List<PmsProductCategoryVo> pmsProductCategoryVos = pmsProductCategoryManager.withChildren();
        return ResultData.ok(pmsProductCategoryVos,"");
    }
}
